package com.blunka.framework.client.ui;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;

import com.blunka.framework.client.constants.ImageConstant;
import com.blunka.framework.client.ui.dialog.HelpDialog;
import com.blunka.framework.client.ui.server.MainFrameServer;
import com.blunka.framework.client.ui.server.TreeModelPanelServer;
import com.blunka.framework.client.ui.tools.ActionUtils;
import com.blunka.framework.client.ui.tools.Menu;
import com.blunka.framework.client.utils.DesignUtils;

/**
 * 系统菜单
 *
 * @平台：统一开发平台
 * @文件名称：FrameMenu.java
 * @文件描述：
 * @author xuqiang
 * @日期：2018年5月31日上午9:30:14
 *
 */
public class FrameMenu extends JMenuBar{

	private static final long serialVersionUID = 1L;
	
	private static FrameMenu frameMenu = new FrameMenu();

	public static FrameMenu newInstance(){
		return frameMenu;
	}

	private FrameMenu(){
		JMenu sysMenu = Menu.createMenu("系统",ImageConstant.SYSTEM_IMG);
		sysMenu.add(Menu.createMenuItem("系统设施", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

			}
		},ImageConstant.SETTING));

		sysMenu.add(Menu.createMenuItem("退出", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(DesignUtils.showConfirm(MainFrame.getMainFrame(), "确定需要退出系统？") == JOptionPane.OK_OPTION){
					System.exit(0);
				}
			}
		},ImageConstant.EXIT));
		add(sysMenu);
		JMenu editMenu = Menu.createMenu("编辑",ImageConstant.EDIT);
		add(editMenu);

		JMenu dbMenu = Menu.createMenu("数据库",ImageConstant.DATABASE);
		
		dbMenu.add(Menu.createMenuItem("初始化服务器业务模型", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ActionUtils.initServerDB();
			}
		},ImageConstant.INIT_LOCAL_DB));
		
		dbMenu.add(Menu.createMenuItem("同步服务器模型到本地", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ActionUtils.sameServerDBToLocal();
			}
		},ImageConstant.SAME_SERVER_DB));
		
		dbMenu.addSeparator();
		dbMenu.add(Menu.createMenuItem("备份服务器业务模型", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//ActionUtils.initServerDB();
			}
		},ImageConstant.DB_BAK));
		
		dbMenu.add(Menu.createMenuItem("备份本地器业务模型", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				//ActionUtils.initServerDB();
			}
		},ImageConstant.DB_BAK));
		add(dbMenu);

		JMenu winMenu = Menu.createMenu("窗体",ImageConstant.WINDOW);
		winMenu.add(Menu.createMenuItem("服务器", new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MainFrameServer serverFrame = MainFrameServer.newInstance();
				if(serverFrame.isVisible() == false){
					int windowWidth = serverFrame.getWidth(); //获得窗口宽

					int windowHeight = serverFrame.getHeight(); //获得窗口高

					Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包

					int screenWidth = kit.getScreenSize().width; //获取屏幕的宽

					int screenHeight = kit.getScreenSize().height; //获取屏幕的高

					serverFrame.setLocation(screenWidth/2 - windowWidth/2, screenHeight/2 - windowHeight/2);//设置窗口居中显示
					//重新生成tree结构
					TreeModelPanelServer.newInstance().reBuildTreeModel();
					
					//隐藏窗体的情况下直接显示
					serverFrame.setVisible(true);
				}
			}
		},ImageConstant.SERVER));
		add(winMenu);

		JMenu helpMenu = Menu.createMenu("帮助",ImageConstant.HELP);
		helpMenu.add(Menu.createMenuItem("帮助文档", new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				HelpDialog.newInstance().setVisible(true);
			}
		}));
		add(helpMenu);
	}
}
